import { http } from "@/utils/http";

/**
 * 用户分数记录查询命令
 * 对应 Java: UserScoreRecordQuery.java
 */
export interface UserScoreRecordListCommand extends BasePageQuery {
  userId?: number; //
  deptId?: number; // 部门ID
  assessmentYear?: string; // 考核年度
  researchTeachingWorkload?: number; //
  educationWorkload?: number; //
  serviceWorkload?: number; //
  otherWorkload?: number; //
  generalCourseScore?: number; //
  majorCourseScore?: number; //
  labCourseScore?: number; //
  pendingScore?: number; //
  approvedTotalScore?: number; //
}

/**
 * 用户分数记录分页响应
 * 对应 Java: UserScoreRecordDTO.java
 */
export interface UserScoreRecordPageResponse {
  id: number; //
  userId: number; //
  userName: string; //
  deptName: string; //
  researchTeachingWorkload: number; //
  educationWorkload: number; //
  serviceWorkload: number; //
  otherWorkload: number; //
  generalCourseScore: number; //
  majorCourseScore: number; //
  labCourseScore: number; //
  pendingScore: number; //
  approvedTotalScore: number; //
  createTime: string; // (Java Date 对应 string)
}

/**
 * 获取用户分数记录列表
 * 对应 Java: UserScoreRecordController.java -> list()
 */
export function getUserScoreRecordListApi(params: UserScoreRecordListCommand) {
  return http.request<ResponseData<PageDTO<UserScoreRecordPageResponse>>>(
    "get",
    "/performance/score/list", //
    {
      params
    }
  );
}

/**
 * 导出用户分数记录Excel
 * 对应 Java: UserScoreRecordController.java -> export()
 */
export const exportUserScoreRecordExcelApi = (
  params: UserScoreRecordListCommand,
  fileName: string
) => {
  return http.download("/performance/score/excel", fileName, { //
    params
  });
};

/**
 * 获取用户分数记录详情
 * 对应 Java: UserScoreRecordController.java -> getInfo()
 */
export function getUserScoreRecordInfoApi(id: number) {
  return http.request<ResponseData<UserScoreRecordPageResponse>>(
    "get",
    `/performance/score/${id}` //
  );
}

/**
 * 删除用户分数记录
 * 对应 Java: UserScoreRecordController.java -> delete()
 * 注意: Java控制器使用了 @RequestBody List<Long> ids,
 * 所以我们使用 'data' 而不是 'params'。
 */
export const deleteUserScoreRecordApi = (data: Array<number>) => {
  return http.request<ResponseData<void>>("delete", "/performance/score", { //
    data // 使用 data 来发送JSON数组 [1, 2, 3]
  });
};

/**
 * 新增用户分数记录命令
 * 对应 Java: AddUserScoreRecordCommand.java
 */
export interface AddUserScoreRecordCommand {
  userId: number; //
  researchTeachingWorkload?: number; //
  educationWorkload?: number; //
  serviceWorkload?: number; //
  otherWorkload?: number; //
  generalCourseScore?: number; //
  majorCourseScore?: number; //
  labCourseScore?: number; //
  pendingScore?: number; //
  approvedTotalScore?: number; //
}

/**
 * 新增用户分数记录
 * 对应 Java: UserScoreRecordController.java -> add()
 */
export const addUserScoreRecordApi = (data: AddUserScoreRecordCommand) => {
  return http.request<ResponseData<void>>("post", "/performance/score", { //
    data
  });
};

/**
 * 更新用户分数记录命令
 * 对应 Java: UpdateUserScoreRecordCommand.java
 */
export interface UpdateUserScoreRecordCommand extends AddUserScoreRecordCommand {
  id: number; //
}

/**
 * 修改用户分数记录
 * 对应 Java: UserScoreRecordController.java -> edit()
 */
export const updateUserScoreRecordApi = (data: UpdateUserScoreRecordCommand) => {
  return http.request<ResponseData<void>>("put", "/performance/score", { //
    data
  });
};

/**
 * 用户分数记录导入失败详情
 */
export interface ImportFailureDetail {
  /**
   * 行号（从1开始）
   */
  rowNumber: number;
  /**
   * 用户ID
   */
  userId: number;
  /**
   * 用户姓名
   */
  nickname: string;
  /**
   * 失败原因
   */
  failureReason: string;
  /**
   * 失败数据
   */
  data: any;
}

/**
 * 用户分数记录导入结果
 */
export interface UserScoreRecordImportResultDTO {
  /**
   * 成功导入数量
   */
  successCount: number;
  /**
   * 失败数量
   */
  failCount: number;
  /**
   * 失败详情列表
   */
  failureDetails: ImportFailureDetail[];
}

/**
 * 下载用户分数记录导入模板
 */
export function downloadUserScoreRecordTemplateApi() {
  return http.download(
    "/performance/score/excelTemplate",
    `用户分数记录导入模板_${new Date().getTime()}.xlsx`
  );
}

/**
 * 导入用户分数记录
 * @param file Excel文件
 */
export function importUserScoreRecordApi(file: File) {
  const formData = new FormData();
  formData.append("file", file);

  return http.request<ResponseData<UserScoreRecordImportResultDTO>>(
    "post",
    "/performance/score/excel",
    {
      data: formData,
      headers: {
        "Content-Type": "multipart/form-data"
      }
    }
  );
}

/**
 * 提交用户分数统计任务
 * 对应 Java: UserScoreRecordController.java -> calculate()
 */
export function calculateUserScoreApi(userId: number) {
  return http.request<ResponseData<void>>(
    "post",
    `/performance/score/calculate/${userId}`
  );
}

/**
 * 批量提交所有用户的分数统计任务
 * 对应 Java: UserScoreRecordController.java -> calculateAll()
 */
export function calculateAllUserScoreApi() {
  return http.request<ResponseData<void>>(
    "post",
    "/performance/score/calculate/all"
  );
}

